#ifndef _SQLITE3_API_H_
#define _SQLITE3_API_H_

#include "./include/sqlite3.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include "a9main.h"

//******************类型定义 *******************************

/* 数据库最大表数目 */
#define TABLE_NAX    20

#define FLAG_CLEAN   1
#define FLAG_RETAUN  2


#define INVALID      0
#define RETRIEVE     1
#define RETRIEVEAND  2
#define RETRIEVEOR   3
#define RETRIEVEALL  4

#define SELECTED     5

#define TYPE_TEXT "text"
#define TYPE_INT  "int"

#define STATE_IN   "入库"
#define STATE_OUT  "出库"


/* 字段定义 */
typedef struct {
	char name[20];
	char type[20];
	char other[50];
	char value[50];
	int flag;
}fields_t;

typedef struct {
	char *tableName;
	fields_t *fields;
}table_t;

typedef struct {
	sqlite3 *db;
	char path[40];
	table_t *tables[TABLE_NAX];
}db_t;


/* 函数声明 */
int open_db(db_t *sqt);
int table_create(db_t *sqt);
int record_insert(db_t *sqt, int tabindex);
int record_delete(db_t *sqt, int tabindex,int limit );
int recordone_delete(db_t *sqt, int tabindex);
int get_real_time(char *times);
int record_update(db_t *sqt, int tabindex);
int record_query(db_t *sqt, int tabindex, int limit);
int recordone_new(db_t *sqt, int tabindex);

int add_goods( db_t *sqt, sendEnv *anamsg);
int del_goods(db_t *sqt, sendEnv *anamsg);
int handle_env(db_t *sqt, sendEnv *anamsg);



//*************************全局变量****************************
#define TABLE_NUM   5                       // 表数量

#define DATABASE_PATH  "/www/cgi-bin/database.db"     //数据库路径


/* 表0 USER 描述 */
#define USER              "user"
#define USER_NO           0

#define USER_name         0
#define USER_password     1

/* 表1 STORAGE1_IN 描述*/
#define STORAGE1IN       "storage1in"
#define STORAGE1IN_NO     1

#define STORAGE1IN_no     1
#define STORAGE1IN_name   2
#define STORAGE1IN_time   3

/* 表2 STORAGE1_OUT 描述*/
#define STORAGE1OUT       "storage1out"
#define STORAGE1OUT_NO    2

#define STORAGE1OUT_no    1
#define STORAGE1OUT_name  2
#define STORAGE1OUT_time  3

/* 表3 STORAGE1_INFO 描述*/
#define STORAGE1INFO       "storage1info"
#define STORAGE1INFO_NO    3

#define STORAGE1INFO_no    1
#define STORAGE1INFO_name  2
#define STORAGE1INFO_state 3
#define STORAGE1INFO_time  4

/* 表4 ENVI1 描述 */
#define ENVI1      "envi1"
#define ENVI1_NO    4

#define ENVI1_temp  1
#define ENVI1_hum   2
#define ENVI1_light 3
#define ENVI1_x     4
#define ENVI1_y     5
#define ENVI1_z     6
#define ENVI1_time  7


#define GOODS_NULL 0
#define GOODS_ADD  1
#define GOODS_SUB  2


#endif

